package com.yequan.jms.p2p.destination;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.util.StringUtils;

import javax.jms.*;

public class Receiver {

    public static void main(String[] args) {
        try {
            ActiveMQConnectionFactory mqConnectionFactory =
                    new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.157.141:61616");
            Connection connection = mqConnectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("test_destination");
            MessageConsumer consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        if (!StringUtils.isEmpty(message)) {
                            TestBean testBean = (TestBean) ((ObjectMessage) message).getObject();
                            System.out.println("收到一条消息--->>>" + testBean.toString());
                        }

                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}
